import utils from '../core/utils';
import logger from '../core/logger';
import current from '../core/current';
import { clientBase } from './core/clientBase';
import { epInfo } from '../models/manga/epInfo';
import { imageToken } from '../models/manga/imageToken';

class imgClient extends  clientBase {
  getUrls(urls: string[]): Promise<imageToken[] | string> {
    return new Promise((resolve) => {
      let content = `urls=${JSON.stringify(urls)}`;
      this.postHttpData('https://manga.bilibili.com/twirp/comic.v1.Comic/ImageToken', content).then((res) => {
        try {
          if (!utils.isEmpty(res) && res.code == '0') {
            let arr: Array<imageToken> = res.data;
            resolve(arr);
          } else
            resolve([]);
        } catch (e) {
          logger.error(e);
          resolve('');
        }
      });
    });
  }

  getImages(id: number): Promise<epInfo | number> {
    return new Promise(async (resolve) => {
      let data = `ep_id=${id}`;
      this.postHttpData('https://manga.bilibili.com/twirp/comic.v1.Comic/GetImageIndex', data).then((res) => {
        try {
          if (res != null && typeof res != 'undefined') {
            if (res.code == '0')
              resolve(res.data);
            else if (res.code == '1') {
              if (utils.isEmpty(current.instance.mid)) {
                utils.showPopup('登录后继续！');
                resolve(0);
              } else
                resolve(2);
            } else
              utils.showPopup('获取信息失败！');
          }
        } catch (e) {
          logger.error(e);
          resolve(-1);
        }
      });
    });
  }
}
export default new imgClient();